//
//  ZYDrawDateView.m
//  Tourism
//
//  Created by 张亚 on 16/1/25.
//  Copyright © 2016年 zhikuiyu. All rights reserved.
//

#import "ZYDrawDateView.h"

#define WeekArray @[@"日",@"一",@"二",@"三",@"四",@"五",@"六"]


@interface ZYDrawDateView ()


@end

@implementation ZYDrawDateView
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        locationIndex = 9;
        rectsArray = [NSMutableArray array];
        [self configure];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self configure];
    }
    return self;
}

- (void)configure{
        self.backgroundColor = [UIColor clearColor];
        upMonth = [NSMutableArray array];
        currentMonth = [NSMutableArray array];
        nextMonth = [NSMutableArray array];
    
        [self prepareForData];
}


- (void)prepareForData{
    [upMonth addObject:@(30)];
    for (int i = 0; i < 30; i++) {
        [currentMonth addObject:@(i + 1)];
    }
    
    int k = 1;
    for (int i = 34; i >= (upMonth.count + currentMonth.count); i--) {
        [nextMonth addObject:@(k)];
    }
}

- (void)drawRect:(CGRect)rect{
    
    CGFloat space = 10;
    CGFloat fontSize = 14;
    //日期的宽度
    CGFloat dayWidth = 30;
    CGFloat dayHeight = 50;
    
    CGFloat between = (self.frame.size.width - space * 2 - dayWidth * 7)/6.0f;
    //日期格式的y值
    CGFloat yValue = 20;
    //横线的y值
    CGFloat lineYValue = yValue + 30;
    
    
    NSMutableParagraphStyle * paragrphStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];//是什么意思
    //画日期格式
    paragrphStyle.alignment = NSTextAlignmentCenter;
    for(int i = 0 ; i < WeekArray.count ; i++){
        NSString * week = [WeekArray objectAtIndex:i];
        if([week length] > 0){
            CGFloat dateFormX = space +  (between + dayWidth) * i;
            CGRect rect = CGRectMake( dateFormX , yValue, dayWidth, fontSize);
            [week drawInRect:rect withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize ],NSParagraphStyleAttributeName:paragrphStyle}];
        }
    }
    
    CGContextRef content = UIGraphicsGetCurrentContext();//获取画板
    CGContextSetFillColorWithColor(content, [UIColor clearColor].CGColor);
    CGContextSetStrokeColorWithColor(content, [UIColor blueColor].CGColor);
    CGContextSetLineWidth(content, 1.0f);
    
    CGContextMoveToPoint(content, 0, 0);
    CGContextAddLineToPoint(content, SCREEN_WIDTH, 0);
    CGContextDrawPath(content, kCGPathFillStroke);
    
    CGContextMoveToPoint(content, 0, lineYValue);
    CGContextAddLineToPoint(content, CGRectGetWidth(rect), lineYValue);
    CGContextDrawPath(content, kCGPathFillStroke);
    
    
    
    if (rectsArray.count > locationIndex) {
        
        NSDictionary * dict = [rectsArray objectAtIndex:locationIndex];
        CGRect rect = [dict[@"rect"] CGRectValue];
        CGContextSetFillColorWithColor(content, [UIColor redColor].CGColor);
        CGContextAddRect(content, rect);
        CGContextDrawPath(content, kCGPathFill);
        
    }
    
    
    //画日期
    CGFloat dayYvalue = yValue + 15 + 30;
    
    //画上一个月的日期
    for (int i = 0; i < upMonth.count; i++) {
        NSNumber * upDay = [upMonth objectAtIndex:i];
        NSString * upMonthDay = [NSString stringWithFormat:@"%@",upDay];
        CGRect rect = CGRectMake(space + (between + dayWidth) * (i % 7) , dayYvalue, dayWidth, fontSize);
        [upMonthDay drawInRect:rect withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize ],NSParagraphStyleAttributeName:paragrphStyle,NSForegroundColorAttributeName:[UIColor grayColor]}];
    }
    
    //画本月的日期
    for (NSInteger i = 0; i < currentMonth.count; i++) {
        NSInteger index = i + upMonth.count;
        
        NSNumber * upDay = [currentMonth objectAtIndex:i];
        NSString * upMonthDay = [NSString stringWithFormat:@"%@",upDay];
        CGRect rect = CGRectMake(space + (between *(index % 7)) + dayWidth * (index % 7) , dayYvalue, dayWidth, fontSize);
        
        //划去每个日期部分的区域
        CGRect midRect = CGRectMake(CGRectGetMidX(rect) - 20, CGRectGetMidY(rect) - 20, 40, 40);
        NSValue * value = [NSValue valueWithCGRect:midRect];
        [rectsArray addObject:@{@"rect":value,@"data":@(i + 1)}];
        
        
        [upMonthDay drawInRect:rect withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize ],NSParagraphStyleAttributeName:paragrphStyle,NSForegroundColorAttributeName:[UIColor blackColor]}];
        if ((index + 1) % 7 == 0 && index > 0) {
            dayYvalue += dayHeight;
        }
       
    }
    
    //画下一个月的日期
    for (NSInteger i = 0; i < 7; i++) {
        NSInteger index = (i + upMonth.count + currentMonth.count) % 7;
        if (i > 7 || index == 0) {
            return;
        }
        NSString * upMonthDay = [NSString stringWithFormat:@"%ld",(long)i + 1];
        CGRect rect = CGRectMake(space +  (between) * (index % 7 ) + dayWidth * (index % 7) , dayYvalue, dayWidth, fontSize);
        [upMonthDay drawInRect:rect withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize ],NSParagraphStyleAttributeName:paragrphStyle,NSForegroundColorAttributeName:[UIColor grayColor]}];
    }
    
}

- (void)rectsContextpoint:(CGPoint)point{

    locationIndex = 1;
    for (int i = 0; i < rectsArray.count; i++) {
        NSDictionary * dict = [rectsArray objectAtIndex:i];
        CGRect rect = [dict[@"rect"] CGRectValue];
        if (CGRectContainsPoint(rect, point)) {
            locationIndex = i;
            NSLog(@"选中%@号",[dict objectForKey:@"data"]);
        [self setNeedsDisplay];
            break;
        }
        
    }
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    UITouch * touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    [self rectsContextpoint:point];
    
}




@end
